package com.ruoyi.make.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.make.domain.ScllMaterialRequiredRescord;
import com.ruoyi.make.mapper.ScllProductionMaterialRequisitionMapper;
import com.ruoyi.make.domain.ScllProductionMaterialRequisition;
import com.ruoyi.make.service.IScllProductionMaterialRequisitionService;

import javax.annotation.Resource;

/**
 * 生产领料Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-01
 */
@Service
public class ScllProductionMaterialRequisitionServiceImpl implements IScllProductionMaterialRequisitionService 
{
    @Resource
    private ScllProductionMaterialRequisitionMapper scllProductionMaterialRequisitionMapper;

    /**
     * 查询生产领料
     * 
     * @param scllId 生产领料主键
     * @return 生产领料
     */
    @Override
    public ScllProductionMaterialRequisition selectScllProductionMaterialRequisitionByScllId(Long scllId)
    {
        return scllProductionMaterialRequisitionMapper.selectScllProductionMaterialRequisitionByScllId(scllId);
    }

    /**
     * 查询生产领料列表
     * 
     * @param scllProductionMaterialRequisition 生产领料
     * @return 生产领料
     */
    @Override
    public List<ScllProductionMaterialRequisition> selectScllProductionMaterialRequisitionList(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        return scllProductionMaterialRequisitionMapper.selectScllProductionMaterialRequisitionList(scllProductionMaterialRequisition);
    }

    /**
     * 新增生产领料
     * 
     * @param scllProductionMaterialRequisition 生产领料
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScllProductionMaterialRequisition(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        int rows = scllProductionMaterialRequisitionMapper.insertScllProductionMaterialRequisition(scllProductionMaterialRequisition);
        insertScllMaterialRequiredRescord(scllProductionMaterialRequisition);
        return rows;
    }

    /**
     * 修改生产领料
     * 
     * @param scllProductionMaterialRequisition 生产领料
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScllProductionMaterialRequisition(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        scllProductionMaterialRequisitionMapper.deleteScllMaterialRequiredRescordByScllId(scllProductionMaterialRequisition.getScllId());
        insertScllMaterialRequiredRescord(scllProductionMaterialRequisition);
        return scllProductionMaterialRequisitionMapper.updateScllProductionMaterialRequisition(scllProductionMaterialRequisition);
    }

    /**
     * 批量删除生产领料
     * 
     * @param scllIds 需要删除的生产领料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScllProductionMaterialRequisitionByScllIds(Long[] scllIds)
    {
        scllProductionMaterialRequisitionMapper.deleteScllMaterialRequiredRescordByScllIds(scllIds);
        return scllProductionMaterialRequisitionMapper.deleteScllProductionMaterialRequisitionByScllIds(scllIds);
    }

    /**
     * 删除生产领料信息
     * 
     * @param scllId 生产领料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScllProductionMaterialRequisitionByScllId(Long scllId)
    {
        scllProductionMaterialRequisitionMapper.deleteScllMaterialRequiredRescordByScllId(scllId);
        return scllProductionMaterialRequisitionMapper.deleteScllProductionMaterialRequisitionByScllId(scllId);
    }

    /**
     * 新增生产领料所需物料信息
     * 
     * @param scllProductionMaterialRequisition 生产领料对象
     */
    public void insertScllMaterialRequiredRescord(ScllProductionMaterialRequisition scllProductionMaterialRequisition)
    {
        List<ScllMaterialRequiredRescord> scllMaterialRequiredRescordList = scllProductionMaterialRequisition.getScllMaterialRequiredRescordList();
        Long scllId = scllProductionMaterialRequisition.getScllId();
        if (StringUtils.isNotNull(scllMaterialRequiredRescordList))
        {
            List<ScllMaterialRequiredRescord> list = new ArrayList<ScllMaterialRequiredRescord>();
            for (ScllMaterialRequiredRescord scllMaterialRequiredRescord : scllMaterialRequiredRescordList)
            {
                scllMaterialRequiredRescord.setScllId(scllId);
                list.add(scllMaterialRequiredRescord);
            }
            if (list.size() > 0)
            {
                scllProductionMaterialRequisitionMapper.batchScllMaterialRequiredRescord(list);
            }
        }
    }
}
